var QQMapWX = require('../../assets/js/qqmap-wx-jssdk');
var qqmapsdk;
const API = require('../../config/API.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: '',
    weatherhours: {},
    weatherlist: {
      weathernow: {},
      weatherweek: {},
      weatherhours: {},
      weatherindices: {},
      weatheraddress: {},
      weathertext: ''
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'PZNBZ-5ARCX-YZM4Y-TX5TE-Y2Q6J-GHBQ6'
    });
    let that = this
    wx.getLocation({
      altitude: 'wgs84',
      success: res => {
        const latitude = res.latitude
        const longitude = res.longitude
        that.setData({
          location: longitude + ',' + latitude
        })
        this.getWeatherList()
      }
      })
  },
  getWeatherList(){
        wx.request({
            url: API.nowweather,
            method: 'GET',
            data: {
              location: this.data.location
            },
            success: res => {
              this.setData({
                'weatherlist.weathernow': res.data
              })
            }
          }),
          wx.request({
            url: API.weekweather,
            method: 'GET',
            data: {
              location: this.data.location
            },
            success: res => {
              this.setData({
                'weatherlist.weatherweek': res.data
              })
            }
          }),
          wx.request({
            url: API.hourweather,
            method: 'GET',
            data: {
              location: this.data.location
            },
            success: res => {
              this.setData({
                'weatherlist.weatherhours': res.data,
                weatherhours: res.data
              })
              this.data.weatherlist.weatherhours.hourly.forEach(element => {
                if (element.text.indexOf('雨') === -1) {
                  this.setData({
                    'weatherlist.weathertext': '多云未来24小时无降雨'
                  })
                } else {
                  this.setData({
                    'weatherlist.weathertext': '多云未来24小时有降雨'
                  })
                }
              });
            }
          }),
          wx.request({
            url: API.indices,
            method: 'GET',
            data: {
              location: this.data.location,
              type: 0
            },
            success: res => {

              this.setData({
                'weatherlist.weatherindices': res.data
              })
            }
          });
        let locat = this.data.location
        var arr = locat.split(',')
        var res = arr.splice(-1).concat(arr).join(',')
        qqmapsdk.reverseGeocoder({ //SDK调用
          location: res,
          success:  (res)=> {    
            this.setData({
              'weatherlist.weatheraddress': res.result
            })
          }
        })
      },
  newcurlocation(e){
    if(e.type){
      this.setData({
        location:e.detail.longitude+','+e.detail.latitude
      })
      this.getWeatherList()
    }
  },
  cityclicklocation(e){
    console.log(e);
    console.log(this);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})